package io.renren.modules.app.indexController;

import com.alibaba.fastjson.JSONObject;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import io.renren.modules.app.entity.company.YcCompanyEntity;
import io.renren.modules.app.entity.company.YcUserCollectJob;
import io.renren.modules.app.entity.index.IndexRecruitEntity;
import io.renren.modules.app.entity.info.CollectData;
import io.renren.modules.app.service.index.YcRecruitService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController//可以标注类只返回结果数据不跳转页面
@RequestMapping("/index/")
@RequiredArgsConstructor//构造方法注入
public class CIndexController {

    private final YcRecruitService ycRecruitService;


    /**
     * 获取首页列表数据
     * @return R
     */
    @RequestMapping("list")
    public R getIndexDataList (@RequestParam Map<String, Object> params) {
        JSONObject entities = ycRecruitService.indexListData(params);
        return R.ok().put("page", entities);//"page"键，返回前端，后端的值entities
    }

    /**
     * 通过招聘编号查询企业编号获取企业详情
     */
    @RequestMapping("company/detail")
    public R getCompanyDetail (@RequestParam("id") String id) {//@RequestParam("id")是前端传过来的id
        R result = R.ok();
        //查询企业详情
        YcCompanyEntity company = ycRecruitService.getCompanyDetail(id);
        result.put("company", company);
        //查询相似职位
        Map<String, Object> params = new HashMap<>();
        params.put("listNotId", id);//查询条件，把当前选中的招聘信息过滤掉，保留其余的
        //getRecruitById(id)：根据招聘id查询招聘信息 ， 单表查询
        //getPositionId()：职位种类id，单表查询， 通过招聘信息查询招聘id，查询职位id，带入sql中查询
        params.put("position", ycRecruitService.getRecruitById(id).getPositionId());
        //查询全部竞价
        List<IndexRecruitEntity> list = ycRecruitService.selectRecruitAllList(params);
        result.put("list", list);
        //通过公司编号查询招聘人信息,多表查询
        result.put("companyUser", ycRecruitService.getCompanyUser(company.getId()));
        //查询招聘福利待遇
        result.put("welfare", ycRecruitService.getRecruitWelfare(id));
        return result;
    }

    /**
     * 查询公司招聘信息
     * @param companyId
     * @return
     */
    @RequestMapping("company/recruit")
    public @ResponseBody R getCompanyRecruit (@RequestParam("companyId") String companyId){
        Map<String, Object> param = new HashMap<>();
        param.put("companyId", companyId);
        return R.ok().put("recruits", ycRecruitService.selectRecruitAllList(param));
    }

    /**
     * 查询用户首页数据
     */
    @RequestMapping("query/user/index/data")
    public R queryCollectCompanyList (@RequestParam("userId") String userId) {
        List<YcUserCollectJob> collectJobs = ycRecruitService.queryCollectJobList(userId);
        return R.ok().put("collectJobs", collectJobs.size());
    }

    /**
     * 查询收藏状态
     */
    @RequestMapping("query/collect")
    public R queryCollectCompany (@RequestParam("type") String type,
            @RequestParam("resumeId") String resumeId, @RequestParam("userId") String userId) {
        boolean bool = ycRecruitService.queryCollect(type, resumeId, userId);
        return R.ok().put("data", bool);
    }

    /**
     * 收藏
     */
    @RequestMapping("save/collect")
    public R saveCollectCompany (@RequestParam("type") String type,
            @RequestParam("resumeId") String resumeId, @RequestParam("userId") String userId) {
        boolean bool = ycRecruitService.addCollect(type, resumeId, userId);
        return R.ok().put("data", bool);
    }

    /**
     * 取消收藏
     */
    @RequestMapping("un/collect")
    public R unCollectCompany (@RequestParam("type") String type,
            @RequestParam("resumeId") String resumeId, @RequestParam("userId") String userId) {
        boolean bool = ycRecruitService.unCollect(type, resumeId, userId);
        return R.ok().put("data", bool);
    }

    /**
     * 查询举报状态
     */
    @RequestMapping("query/complaint")
    public R queryComplaint (@RequestParam("companyId") String companyId,
            @RequestParam("userId") String userId) {
        boolean bool = ycRecruitService.queryComplaint(companyId, userId);
        return R.ok().put("data", bool);
    }

    /**
     * 添加举报数据
     */
    @RequestMapping("save/complaint")
    public R saveComplaint (@RequestParam("companyId") String companyId,
            @RequestParam("userId") String userId, @RequestParam("content") String content) {
        boolean bool = ycRecruitService.doComplaint(companyId, userId, content);
        return R.ok().put("data", bool);
    }

    /**
     * 查询收藏列表
     */
    @RequestMapping("query/collect/list")
    public R queryCollectList (@RequestParam Map<String, Object> params) {
        List<CollectData> collectDataList = new ArrayList<>();
        if (Objects.nonNull(params) && !params.isEmpty()) {
            collectDataList = ycRecruitService.queryCollectList(String.valueOf(params.get("userId")));
        }
        return R.ok().put("data", collectDataList);
    }
}
